from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from ..models import *
from django.core.urlresolvers import reverse
from django.contrib.auth.hashers import make_password, check_password
from project_1.settings import BASE_DIR
import json
from django.http import Http404
from django.contrib.auth.decorators import permission_required

# 商品分类主页
@permission_required('myadmin.show_cate')
def index(request):
	# 相当于sql的  select *,concat(path,id) as paths from Cates order by paths;
	cate = Cates.objects.filter(isDelete=False).extra(select={'paths':'concat(path,id)'}).order_by('paths')
	val = request.GET.get('val')
	if val:
		try:
			cate = Cates.objects.get(isDelete=False,name=val)
			cate = Cates.objects.filter(isDelete=False,pid=cate.id)
		except:
			cate = []

	from django.core.paginator import Paginator
	# 实例化分页类
	paginator = Paginator(cate,10)

	# 获取当前页码
	try:
		p = int(request.GET.get('p',1))
	except:
		p = 1

	# 获取分页数据对象[{id:1},{id:2}]  [{id:3},{id:4}]
	goodslist= paginator.page(p)
	for i in goodslist:
		if i.pid !=0:
			i.fen = Cates.objects.get(id=i.pid).name
	return render(request,'myadmin/cates/index.html',{"data":goodslist})
# 商品添加分类
@permission_required('myadmin.insert_cate')
def add(request):
	try:
		if request.method == "GET":
			data = Cates.objects.filter(isDelete=False)
			return render(request,'myadmin/cates/add.html',{"data":data})
		else:
			data = Cates()
			reda = request.POST.dict()
			reda.pop('csrfmiddlewaretoken')
			counts = Cates.objects.filter(name=reda['name']).exclude(isDelete=True)
			if len(counts):
				return HttpResponse("<script>alert('添加失败!商品分类名不能重复');location.href='"+reverse('myadmin_cates_add')+"'</script>")
			else:
				print(reda)
				data.name = reda['name']
				data.pid = reda['pid']
				if reda['pid'] == "0":
					data.path="0,"
				else:
					res = Cates.objects.get(id=data.pid)
					data.path = res.path+str(res.id)+","
					print(data.pid,data.path)
				data.save()
			return HttpResponse("<script>alert('添加成功');location.href='"+reverse('myadmin_cates_index')+"'</script>")
	except:
		return HttpResponse("<script>alert('添加失败');location.href='"+reverse('myadmin_cates_index')+"'</script>")
# 删除商品分类
@permission_required('myadmin.delete_cate')
def delete(reqeust):
	cid = reqeust.GET.get('id',0)
	cate = Cates.objects.get(id=cid)
	cun = Cates.objects.filter(pid=cate.id)
	if len(cun) == 1 and cate.pid == 0:
		cate.isDelete = 1
		cate.save()
		return JsonResponse({"msg":"删除成功","error":1})
	if cun:
		return JsonResponse({"msg":"无法删除","error":0})
	else:
		cate.isDelete = 1
		cate.save()
		return JsonResponse({"msg":"删除成功","error":1})


# 修改
@permission_required('myadmin.show_update')
def update(request):
	try:
		cate = Cates.objects.get(id=int(request.GET.get('cid')))
		cate.name = request.GET.get('cname')
		cate.save()
		return JsonResponse({"msg":"修改成功","error":0})
	except:
		return JsonResponse({"msg":"修改失败","error":1})
# 动态查询
def query(request):
	cid = request.GET.get("id")
	data = Cates.objects.filter(pid=cid).exclude(isDelete=1).values()
	return JsonResponse(list(data),safe=False)
